package com.yingda.activity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Telephony;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.hjq.toast.ToastUtils;
import com.yingda.ad.R;
import com.yingda.bean.MessageBean;
import com.yingda.utils.SendMessageUtils;
import com.yingda.utils.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

public class MessgeReadActivity extends BaseActivity {
    private static final String TAG = "MessgeReadActivity";
    private Handler handler;
    private Runnable runnable;
    private static final long INTERVAL = 1000 * 60; // 2秒

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_layout);
        TextView tv_phone = findViewById(R.id.tv_phone);
        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取多个手机卡
                getPhoneNumbers();


            }
        });


        //获取本人的手机号
        String defaultPhone = SharedPreferenceUtil.getInstance(this).getString("defaultPhone");
        String newPhone = SharedPreferenceUtil.getInstance(this).getString("newPhone");
        handler = new Handler(Looper.getMainLooper());
        runnable = new Runnable() {
            @Override
            public void run() {
                // 在这里执行你的定时任务
              //  Toast.makeText(MessgeReadActivity.this, "定时任务执行", Toast.LENGTH_SHORT).show();
                if (!TextUtils.isEmpty(defaultPhone)){
                    SendMessageUtils.getInstance().getPhone(MessgeReadActivity.this,defaultPhone);
                }
                if (!TextUtils.isEmpty(newPhone)){
                    SendMessageUtils.getInstance().getPhone(MessgeReadActivity.this,newPhone);
                }
                // 继续下一次调用
                handler.postDelayed(this, INTERVAL);
            }
        };
      //  SendMessageUtils.getInstance().setMessage("18712654567","1233","18765436787");
     //   SendMessageUtils.getInstance().getPhone(this,"18712654567");
        // 开始定时任务
        handler.postDelayed(runnable, INTERVAL);


    }

    private void getPhoneNumbers() {
        List<String> phones = new ArrayList<>();
        SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ToastUtils.show("权限没有");
            return;
        }
        List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
        if (subscriptionInfoList != null && !subscriptionInfoList.isEmpty()) {
            for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
                //获取手机号
                String phoneNumber = subscriptionInfo.getNumber();
                phones.add(phoneNumber);
                // 获取运营商名称
                String carrierName = subscriptionInfo.getCarrierName().toString();
                // 处理每个手机号
               // Toast.makeText(this, "Phone Number: " + phoneNumber, Toast.LENGTH_LONG).show();
            }

            ToastUtils.show("获取手机号成功");
        } else {
            Toast.makeText(this, "SIM 没有找到", Toast.LENGTH_SHORT).show();
        }
        if (phones.size()  ==1){
            String phone = phones.get(0);
            SharedPreferenceUtil.getInstance(this)
                    .setString("defaultPhone",phone);
        }

        if (phones.size() == 2){
            String phone = phones.get(0);
            SharedPreferenceUtil.getInstance(this)
                    .setString("defaultPhone",phone);

            String phone1 = phones.get(1);
            SharedPreferenceUtil.getInstance(this)
                    .setString("newPhone",phone1);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止定时任务
        handler.removeCallbacks(runnable);
    }
}
